package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangtai.Neusoft.vo.ContractVo;
import com.zhangtai.modules.dto.ContractNewDto;
import com.zhangtai.modules.dto.ContractRenewDto;
import com.zhangtai.modules.dto.ContractTerminateDto;
import com.zhangtai.modules.entity.contract.ContractMangerEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-20 17:57:00
 */
@Mapper
public interface ContractMangerDao extends BaseMapper<ContractMangerEntity> {

    @Select("<script>SELECT " +
            "t1.id,t1.oid,t1.type,t5.code,t5.name,t5.org_id,t5.unit_id,t6.contract_type,t6.employer_company," +
            "t6.contract_status status,t6.contract_begin_date,t6.contract_end_date," +
            "t1.contract_begin_date end_time,t1.sign_date end_sign_date,t6.contract_oss_url begin_contractUrl," +
            "t1.contract_oss_url end_contractUrl,case t1.operate_type when '2' then 1 when '3' then 1 when '5' then 2 else t1.operate_type end end_type," +
            "t1.operate_type,t1.contract_type,t6.contract_time_type,t5.employeestatus," +
            "t1.renew_contract_id,t6.employer_company_id company_id,t1.esign_flow_id,t5.dimission_time " +
            "FROM tb_contract_manger t1 " +
            "LEFT JOIN tb_employee_info t5 on t5.oid = t1.oid " +
            "LEFT JOIN tb_contract_manger t6 ON t1.renew_contract_id = t6.id " +
            "<where> t1.is_delete = 0 " +
            "<if test='code!=null and code !=&apos;&apos;'> and t5.code like '%${code}%'</if>" +
            "<if test='name!=null and name !=&apos;&apos;'> and t5.name like '%${name}%'</if>" +
            "<if test='contractType!=null'> and t1.contract_type = #{contractType}</if>" +
            "<if test='contractTimeType!=null'> and t1.contract_time_type = #{contractTimeType}</if>" +
            "<if test='status!=null'> and t1.contract_status = #{status}</if>" +
//            "<if test='operateType!=null'> and t6.operate_type = #{operateType}</if>" +
            "<if test='contractStatus!=null and contractStatus.size>0'> and t1.contract_status in " +
            "<foreach item=\"s\" index=\"index\" collection=\"contractStatus\" open=\"(\" separator=\",\" close=\")\">#{s}</foreach>" +
            "</if>"+
            "<if test='orgId!=null and orgId.size>0'> and t5.org_id in " +
            "<foreach item=\"org\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{org}</foreach>" +
            "</if>"+
            "<if test='unitId!=null and unitId.size>0'> and t5.unit_id in " +
            "<foreach item=\"unit\" index=\"index\" collection=\"unitId\" open=\"(\" separator=\",\" close=\")\">#{unit}</foreach>" +
            "</if>"+
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            "<foreach item=\"ids\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{ids}</foreach>" +
            "</if>"+
            "<if test='orgIds.size==0 or orgIds==null'> and t1.unit_id = null </if>"+
            "<if test='orgId !=null and orgId.size==0'> and t5.org_id =null </if>" +
            "<if test='unitId !=null and unitId.size==0'> and t5.unit_id =null </if>" +
            "<if test='endType !=null and endType == &quot;1&quot;'> " +
            "   and (t1.operate_type = 2 or t1.operate_type = 3 or t1.operate_type = 7) and (t1.type = 3 or t1.type = 5)" +
            "</if>" +
            "<if test='endType !=null and endType ==&quot;2&quot; '> and t1.operate_type = 5 and t1.type = 3</if>" +
            "</where>" +
            " order by t1.create_time desc" +
            "</script>")
    List<ContractTerminateDto> getTerminateList(@Param("code") String code,
                                                @Param("name") String name,
                                                @Param("orgId")List<Long> orgId,
                                                @Param("unitId")List<Long> unitId,
                                                @Param("contractType")Integer contractType,
                                                @Param("contractTimeType")Integer contractTimeType,
                                                @Param("operateType") Integer operateType,
                                                @Param("status") Integer status,
                                                @Param("orgIds")List<Long> orgIds,
                                                @Param("contractStatus")List<Integer> contractStatus,
                                                @Param("endType")String endType,
                                                IPage<ContractTerminateDto> page);

    @Select("<script>SELECT t1.id,t1.oid,t1.type,t2.code,t2.name,t2.org_id,t1.contract_type,t2.job_type," +
            "t2.unit_id,t2.gender,t2.job_id,t2.hiredate,t1.is_reinstate,t1.operate_type,t1.contract_begin_date new_contract_begin_date," +
            "t1.esign_flow_id  " +
            "FROM tb_contract_manger t1 " +
            "LEFT JOIN tb_employee_info t2 on t1.oid = t2.oid" +
            "<where> t1.is_delete = 0 and (t1.type = 1 or t1.type = 4) " +
            "<if test='code!=null and code !=&apos;&apos;'> and t2.code like '%${code}%'</if>" +
            "<if test='name!=null and name !=&apos;&apos;'> and t2.name like '%${name}%'</if>" +
            "<if test='contractStatus!=null and contractStatus.size>0'> and t1.contract_status in " +
            "<foreach item=\"s\" index=\"index\" collection=\"contractStatus\" open=\"(\" separator=\",\" close=\")\">#{s}</foreach>" +
            "</if>"+
            "<if test='jobId!=null and jobId.size>0'> and t2.job_id in " +
            "<foreach item=\"job\" index=\"index\" collection=\"jobId\" open=\"(\" separator=\",\" close=\")\">#{job}</foreach>" +
            "</if>"+

            "<if test='unitId!=null and unitId.size>0'> and t2.unit_id in " +
            "<foreach item=\"unit\" index=\"index\" collection=\"unitId\" open=\"(\" separator=\",\" close=\")\">#{unit}</foreach>" +
            "</if>"+
            "<if test='orgId!=null and orgId.size>0'> and t2.org_id in " +
            "<foreach item=\"o\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{o}</foreach>" +
            "</if>"+
            "<if test='jobId !=null and jobId.size==0'> and t2.job_id =null </if>" +
            "<if test='unitId !=null and unitId.size==0'> and t2.unit_id =null </if>" +
            "<if test='orgId !=null and orgId.size==0'> and t2.org_id =null </if>" +
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            "<foreach item=\"ids\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{ids}</foreach>" +
            "</if>"+
            "<if test='orgIds.size==0 or orgIds==null'> and t1.unit_id = null </if>"+
            "<if test='hiredate!=null'> and t2.hiredate &gt;= #{hiredate} </if>" +
            " and t2.employeestatus != 3  "+
            "<if test='isApply!=null and isApply '> and t2.employeestatus != '3' </if>"+
            " order by t2.hiredate desc" +
            "</where>" +
            "</script>")
    List<ContractNewDto> getMangerList(@Param("name") String name,
                                             @Param("code") String code,
                                             @Param("jobId")List<Long> jobId,
                                             @Param("unitId")List<Long> unitId,
                                             @Param("orgId")List<Long> orgId,
                                             @Param("hiredate")String hiredate,
                                             @Param("contractStatus")List<Integer> contractStatus,
                                             @Param("orgIds")List<Long> orgIds,
                                            @Param("isApply")boolean isApply,
                                             IPage<ContractNewDto> page);

    @Select("<script>" +
            "SELECT " +
            "t1.id,t1.oid,t1.code,t1.type,t1.employer_company,t1.employer_company_id,t4.labor_contract_years,t4.name,t4.hiredate,t3.contract_type,t1.contract_type," +
            "t3.contract_begin_date,t3.contract_end_date,t3.contract_status,t1.operate_type,t3.contract_oss_url contract_esign_url," +
            "t1.contract_begin_date new_contract_begin_date,t1.esign_flow_id,t4.org_id,t4.unit_id,t4.job_id " +
            "FROM  tb_contract_manger t1 " +
            "LEFT JOIN tb_contract_dic t2 ON t1.contract_type = t2.type " +
            "LEFT JOIN tb_contract_manger t3 on t3.id = t1.renew_contract_id " +
            "LEFT JOIN tb_employee_info t4 on t4.oid = t1.oid " +
            "<where>t1.is_delete =0 and t1.type = 2 " +
            "<if test='code!=null and code !=&apos;&apos;'> and t4.code like '%${code}%'</if>" +
            "<if test='name!=null and name !=&apos;&apos;'> and t4.name like '%${name}%'</if>" +
            "<if test='contractStatus!=null and contractStatus.size>0'> and t1.contract_status in " +
            "<foreach item=\"s\" index=\"index\" collection=\"contractStatus\" open=\"(\" separator=\",\" close=\")\">#{s}</foreach>" +
            "</if>"+
            "<if test='jobId!=null and jobId.size>0'> and t4.job_id in " +
            "<foreach item=\"job\" index=\"index\" collection=\"jobId\" open=\"(\" separator=\",\" close=\")\">#{job}</foreach>" +
            "</if>"+
            "<if test='unitId!=null and unitId.size>0'> and t4.unit_id in " +
            "<foreach item=\"unit\" index=\"index\" collection=\"unitId\" open=\"(\" separator=\",\" close=\")\">#{unit}</foreach>" +
            "</if>"+
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            "<foreach item=\"ids\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{ids}</foreach>" +
            "</if>"+
            "<if test='orgId!=null and orgId.size>0'> and t4.org_id in " +
            "<foreach item=\"o\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{o}</foreach>" +
            "</if>"+
            "<if test='orgId !=null and orgId.size==0'> and t4.org_id =null </if>" +
            "<if test='status!=null'> and t1.contract_status = #{status}</if>" +
            "<if test='orgIds.size==0 or orgIds==null'> and t1.unit_id = null </if>"+
            "<if test='hiredate!=null'> and t4.hiredate &gt;= #{hiredate} </if>"+
            "<if test='jobId !=null and jobId.size==0'> and t4.job_id =null </if>" +
            "<if test='unitId !=null and unitId.size==0'> and t4.unit_id =null </if>" +
            "<if test='isApply!=null and isApply '> and t4.employeestatus != '3' </if>"+
            "</where>" +
            " order by t1.create_time desc" +
            "</script>")
    List<ContractRenewDto> getContinueMangerList(@Param("name") String name,
                                                 @Param("code") String code,
                                                 @Param("jobId")List<Long> jobId,
                                                 @Param("unitId")List<Long> unitId,
                                                 @Param("orgId")List<Long> orgId,
                                                 @Param("hiredate")String hiredate,
                                                 @Param("contractStatus")List<Integer> contractStatus,
                                                 @Param("status")Integer status,
                                                 @Param("orgIds")List<Long> orgIds,
                                                 @Param("isApply")boolean isApply,
                                                 IPage<ContractRenewDto> page);

    @Select("Select t1.* from tb_contract_manger t1 left join tb_employee_info t2 on t1.oid = t2.oid and t2.employeestatus = 2 where (t1.type = 1 or t1.type = 2 or t1.type=4 ) " +
            "and t1.contract_status = 1 and TIMESTAMPDIFF(DAY,#{date},t1.contract_end_date) <=40  and is_renew = 0")
    List<ContractMangerEntity> getDrawToRenewList(@Param("date")String date);

    @Update("update tb_contract_manger set is_delete = 1 where oid = #{oid} and type = 2 and contract_status = 0")
    void removeRenew(@Param("oid") Long oid);

    @Select("<script>SELECT\n" +
            "\ttcm.*\n" +
            "FROM\n" +
            "\ttb_contract_manger tcm\n" +
            " <where> tcm.is_delete = 0 " +
            "  and tcm.oid = #{oid} " +
            "</where>" +
            " order by tcm.create_time desc" +
            "</script>")
    List<ContractTerminateDto> getContractMangerMemberList(@Param("oid") Long oid,
                                                IPage<ContractTerminateDto> page);

    @Select("select * from tb_contract_manger t1 where t1.operate_type = 7 and t1.contract_status = 0 and t1.oid = #{oid}")
    ContractMangerEntity getUserContract(Long oid);

    @Select("select t2.contract_no as code from tb_contract_record t1 left join tb_contract_manger t2 on t1.start_contract_id = t2.id where t1.end_time = #{date}")
    List<ContractVo> getRelieveList(String date);

}
